/*Problem Id:1004  User Id:team01 
Memory:104K  Time:76MS
Language:C++  Result:Accepted

	problem name: Tangled in Cables
	source : Mid-Atlantic 2004
	problem type: 图论 求图的最小生成树
	
	date : 2005.4.23 北师大校内比赛


Source 
*/
//who? maybe mime
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
char name[10000][30];
int visit[10000];
struct li
{
int x;
int y;
double dis;
}line[10000];
bool lesst(const li &p1,const li &p2)
{
return p1.dis<p2.dis;
}
int main()
{
int i,j,k,pnum,lnum,m,n;
double alllength,sum;
char tempname[30];
li *start,*end; 
cin>>alllength;
cin>>pnum;
for(i=0;i<pnum;i++)
   cin>>name[i];
cin>>lnum;
for(i=0;i<lnum;i++)
{
  cin>>tempname;
  for(j=0;j<pnum;j++)
  {
     if(strcmp(name[j],tempname) == 0)
        break;
  }
  line[i].x = j;
  cin>>tempname;
  for(j=0;j<pnum;j++)
  {
     if(strcmp(name[j],tempname) == 0)
        break;
  }
  line[i].y = j;
  cin>>line[i].dis;
}
memset(visit,0,pnum*sizeof(int));
start = line;
end = &line[lnum];
sort(start,end,lesst);
visit[0] = 1;
sum = 0;
for(i=0;i<pnum;i++)
{
  for(j=0;j<lnum;j++)
  {
    if(((visit[line[j].x]==0)&&(visit[line[j].y]))||((visit[line[j].y]==0)&&(visit[line[j].x])))
    {
       sum+=line[j].dis;
       visit[line[j].x] = 1;
       visit[line[j].y] = 1;
       break;
    }
  }
  if(j==lnum)
     break;
}
if((i==pnum-1)&&(sum<=alllength))
   printf("Need %.1f miles of cable\n",sum);
else
   printf("Not enough cable\n");

return 0;
}


